package com.dy.security.session;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.security.web.session.InvalidSessionStrategy;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: ding-yu
 * @Date: 2022/5/23 16:54
 * @Desctiption: (描述) 处理会话过期后的提示
 */
public class MyInvalidSessionStrategy implements InvalidSessionStrategy {
    @Override
    public void onInvalidSessionDetected(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        response.setContentType("application/json; charset=UTF-8");
        Map<String,Object> result = new HashMap<>();
        result.put("status",500);
        result.put("msg","当前会话已经失效，请重新登录");
        response.getWriter().print(new ObjectMapper().writeValueAsString(result));
        response.flushBuffer();
    }
}
